//
//  EthTransactionSignParams.m
//  Test
//
//  Created by xgc on 1/5/18.
//  Copyright © 2018 xgc. All rights reserved.
//

#import "EthTransactionSignParams.h"


bool str2U256(string str, u256& value) {
    
    stringstream strValue;
    strValue << str;
    strValue >> value;

    return true;
}

@implementation EthTransactionSignParams

-(EthCreateSignTransactionParams*) toEthCreateSignTransactionParams
{
    EthCreateSignTransactionParams* signParams = new EthCreateSignTransactionParams();
    
    BtSeed* seed = [self.btSeed toBtSeed];
    signParams->setBtSeed(*seed);
    delete seed;
    
    string password = [self.password UTF8String];
    signParams->setPassword(password);
    
    string from = [self.from UTF8String];
    signParams->setFrom(from);
    
    string to = [self.to UTF8String];
    signParams->setTo(to);
    
    /*
    string data = [self.data UTF8String];
    signParams->setData(data);
     */
    std::vector<uint8_t> vData;
    if(self.data) {
        void *buffer = (void*)[self.data bytes];
        size_t len = [self.data length];
        
        vData.assign((uint8_t*)buffer, (uint8_t*)buffer + len);
    }

    signParams->setData(vData);
    
    string priKey = [self.priKey UTF8String];
    signParams->setPriKey(priKey);
    
    string gasLimit = [self.gasLimit UTF8String];
    u256 ugasLimit;
    str2U256(gasLimit, ugasLimit);
    
    signParams->setGasLimit(ugasLimit);
    
    string gasPrice = [self.gasPrice UTF8String];
    u256 ugasPrice;
    str2U256(gasPrice, ugasPrice);
    
    signParams->setGasPrice(ugasPrice);
    
    string value = [self.value UTF8String];
    u256 uValue;
    str2U256(value, uValue);
    
    signParams->setValue(uValue);
    
    string nonce = [self.nonce UTF8String];
    u256 uNonce;
    str2U256(nonce, uNonce);
    
    signParams->setNonce(uNonce);
    
    return signParams;
}

-(id)init {
    if (self = [super init]) {
        self.gasLimit = @"0";
        self.gasPrice = @"0";
        self.value = @"0";
        self.nonce = @"0";
    }
    return self;
}

@end
